<?php
	if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Users_Ctrl extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
			}
		function index($query_id=0, $order_by  = 'created_date', $sort_order = 'desc', $current_page = 0){
			$search_by='';
			$search_by_value='';
				if($query_id == '0'){		
					//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					}
				}
			
			$results_per_page = 4;
			
			$url = CURL_URL;
			$data = array('www-command'=>'adminuser-all_adminusers','current_page'=>$current_page,'results_per_page'=>$results_per_page,'sort_order'=>$sort_order,'order_by'=>$order_by,'search_by'=>$search_by,'search_by_value'=>$search_by_value);
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response = json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response->message);exit;
			if($response->message !== 'No Admin User'){
			curl_close($ch);
			
			$this->load->library('pagination');			
			$config['base_url'] = base_url().'admin/users_ctrl/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows']  = $response->total;
			$config['uri_segment'] = 7;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['adminuser_all_adminuser'] = $response;
			$data['user'] = '';
			}else{
				$data['user'] = 1;
				$data['adminuser_all_adminuser'] = '';
				//echo '<pre>';print_r($data);exit;
			}
			$data['users_fileds'] = array('first_name'=>'First Name', 'last_name'=>'Last Name', 'email'=>'Email','user_id'=>'User Id','status'=>'Status','created_date'=>'Date');
			$data['order_by'] = $order_by;
			$data['sort_order'] = $sort_order;
			$data['query_id'] = $query_id;
			$data['status'] = array(1=>'Active', 0=>'In-active');
			$data['search_by'] = $search_by;
			$data['search_by_value'] = $search_by_value;
			//echo '<pre>';print_r($data);exit;
			$this->load->view('admin/users_view',$data);
		}
		
		function search_by(){
			if($this->input->post('search') == ''){
				$search = $this->input->post('status');
			}else{
				$search = $this->input->post('search');	
			}
				
			$query_array = array(
				'search_by'=>$this->input->post('search_by'),
				'search'=>$search
			);
			$query_id=rand();
			$_SESSION['search']=$query_array;
			//echo '<pre>';print_r($_SESSION['search']);exit;
			redirect('admin/users_ctrl/index/'.$query_id);
			
		}		
		
	
			
		function create_users_page(){
			$url = CURL_URL;
				$data = array('www-command'=>'adminuser-get_adminuser_roles');
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				curl_close($ch);
				$data['adminuser_get_adminuser_roles'] = $response;
			$this->load->view('admin/create_users_page', $data);
			//$this->load->view('admin/edit_user_page', $data);
		}
		
		function create_user(){
			$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('first_name', 'Firstname', 'required|alpha');
				$this->form_validation->set_rules('middle_name', 'Middlename', 'alpha');
				$this->form_validation->set_rules('last_name', 'Lastname', 'required|alpha');
				$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
				$this->form_validation->set_rules('user_id', 'User Id', 'required|alpha_numeric');
				$this->form_validation->set_rules('password', 'Password', 'required|regex_match[/^([a-zA-Z0-9@*#]{8,15})$/]');
				$this->form_validation->set_rules('role', '', 'callback_role');
				
				if($this->form_validation->run() == FALSE){
						$url = CURL_URL;
				$data = array('www-command'=>'adminuser-get_adminuser_roles');
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				curl_close($ch);
				$data['adminuser_get_adminuser_roles'] = $response;
				$this->load->view('admin/create_users_page', $data);
				}else{
					$url = 'http://localhost/azindia/azindia-api/json.api';
					$data = array('www-command'=>'adminuser-create_adminuser', 
				'first_name' => $this->input->post('first_name'),
				'middle_name' => $this->input->post('middle_name'),
				'last_name' => $this->input->post('last_name'),
				'email' => $this->input->post('email'),
				'user_id' => $this->input->post('user_id'),
				'password' => $this->input->post('password'),
				'role_id' => $this->input->post('role')
				);
					$string = http_build_query($data);					
					//echo '<pre>';print_r($chek);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//$data['articles_create_author'] = $response;
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/users_ctrl/index/');
				}
		}
		
		function role($role){
			if($role == ""){
				$this->form_validation->set_message('role','Please select role');
				return false;
			}else{
				return true;
			}
		}
		
		function edit_user_page(){
				$this->load->model('admin_user_model');
				if($this->uri->segment(4) > 0){
					$_SESSION['adminid'] = $this->uri->segment(4);
				}
				$data = array('www-command'=>'adminuser-get_adminuser','admin_id'=>$_SESSION['adminid']);
				//echo '<pre>';print_r($data);exit;
				$adminuser_get_adminuser = $this->admin_user_model->user_data($data);
				$data = array('www-command'=>'adminuser-get_adminuser_roles');
				$adminuser_get_adminuser_roles = $this->admin_user_model->user_data($data);
				$data['adminuser_get_adminuser'] = $adminuser_get_adminuser;
				$data['adminuser_get_adminuser_roles'] = $adminuser_get_adminuser_roles;
				//echo '<pre>';print_r($data);exit;
				$this->load->view('admin/edit_user_page',$data);
				}
		
		function edit_user(){
			$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('first_name', 'Firstname', 'required|alpha');
				$this->form_validation->set_rules('middle_name', 'Middlename', 'alpha');
				$this->form_validation->set_rules('last_name', 'Lastname', 'required|alpha');
				$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
				$this->form_validation->set_rules('user_id', 'User Id', 'required|alpha_numeric');
				$this->form_validation->set_rules('password', 'Password', 'required|regex_match[/^([a-zA-Z0-9@*#]{8,15})$/]');
				$this->form_validation->set_rules('role', '', 'callback_role');
					
				if($this->form_validation->run() == FALSE){
				$this->load->model('admin_user_model');
				$data = array('www-command'=>'adminuser-get_adminuser','admin_id'=>$_SESSION['adminid']);
				//echo '<pre>';print_r($data);exit;
				$adminuser_get_adminuser = $this->admin_user_model->user_data($data);
				$data = array('www-command'=>'adminuser-get_adminuser_roles');
				$adminuser_get_adminuser_roles = $this->admin_user_model->user_data($data);
				$data['adminuser_get_adminuser'] = $adminuser_get_adminuser;
				$data['adminuser_get_adminuser_roles'] = $adminuser_get_adminuser_roles;
				//echo '<pre>';print_r($data);exit;
				$this->load->view('admin/edit_user_page',$data);
				}else{
					$url = 'http://localhost/azindia/azindia-api/json.api';
					$data = array('www-command'=>'adminuser-edit_adminuser', 
				'admin_id' => $this->input->post('hidden'),
				'first_name' => $this->input->post('first_name'),
				'middle_name' => $this->input->post('middle_name'),
				'last_name' => $this->input->post('last_name'),
				'email' => $this->input->post('email'),
				'user_id' => $this->input->post('user_id'),
				'password' => md5($this->input->post('password')),
				'role_id' => $this->input->post('role')
				);
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//$data['articles_create_author'] = $response;
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/users_ctrl/index/');
				}
		}
		
		function delete_user(){
			$url = 'http://localhost/azindia/azindia-api/json.api';
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'adminuser-delete_adminuser','status'=>$status,'admin_id'=>$this->uri->segment(4));
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				
				redirect('admin/users_ctrl/index/');

		}
	}
	
?>